import {
  $wuxToast
} from '../../../es/index'
Page({
  data: {
    page: 1,
    fileList: [],
    hasmore: true, //标志是否还有更多数据
    token: ""
  },

  /**
   * 生命周期函数--监听页面加载
   * 获取第一页的文件下载记录
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      token: options.token
    })
    wx.request({
      //后期改成服务器路径
      // url: 'http://localhost/user/getUploadRecords',
      url: 'https://szsyph.cn/user/getUploadRecords',
      method: "GET",
      header:{
        'cookie':'token='+this.data.token
      },
      data: {
        page: this.data.page
      },
      success(res) {
        var response = res.data;
        console.log(response);
        that.setData({
          fileList: response.data.fileUploadRecords
        });
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasmore == false)
      return;
    var that = this;
    wx.showLoading({
      title: '正在加载'
    });
    var currentPage = this.data.page;
    currentPage++;
    //如果还有更多数据的话进行请求
    wx.request({
      //后期改成服务器路径
      // url: 'http://localhost/user/getUploadRecords',
      url: 'https://szsyph.cn/user/getUploadRecords',
      method: "GET",
      data: {
        token: this.data.token,
        page: currentPage
      },
      success(res) {
        //隐藏loading图标
        wx.hideLoading();
        //获取新一页的数据
        var response = res.data;
        var fileList = that.data.fileList;
        //如果返回码为200说明获取成功
        if (response.code == 200) {
          var fileListNew = response.data.fileUploadRecords;
          if (fileListNew.length == 0) {
            that.setData({
              hasmore: false
            })
            that.showToastCancel();
          } else {
            for (var i = 0; i < fileListNew.length; i++) {
              console.log("加入了新元素")
              fileList.push(fileListNew[i]);
            }
            that.setData({
              page: currentPage,
              fileList: fileList
            });
          }
        }
      }
    });
  },
  //弹出提示框
  showToastCancel() {
    $wuxToast().show({
      type: 'forbidden',
      duration: 1500,
      color: '#fff',
      text: "没有更多内容啦"
    })
  },

})